//
//  HHNavigationStack.swift
//  HHSwiftuiPractice
//
//  Created by YL-Hong on 2023/10/9.
//在 SwiftUI 中,NavigationStack 和 NavigationView 都可用于构建导航界面,主要区别是:

//avigationStack 是新一代导航容器,NavigationView 将被逐步淘汰。
//NavigationStack 使用路径来定义导航状态,更灵活
//更加节省资源


import SwiftUI

struct HHNavigationStack: View {
    private var countries: [String] = ["China", "Japan", "France", "Korea", "United state"]
    @State private var path = NavigationPath()
    
    var body: some View {
        NavigationStack(path: $path) {
                    ScrollView {
                        Button {
                            path.append("UK")
                            path.append("India")
                            path.append("Philippines")
                            path.append(1000)
                        } label: {
                            Text("PUSH")
                                .padding()
                        }

                        VStack(spacing: 40, content: {
                            ForEach(countries, id: \.self) { country in
//                                NavigationLink(value: country) {
//                                    Text("country \(country)")
//                                }
                                Button {
                                    path.append(country)
                                } label: {
                                    Text(country)
//                                        .padding()
                                }
                            }

                            ForEach(0..<10) { i in
                                NavigationLink(value: i) {
                                    Text("Index: \(i)")
                                }
                            }
                        })
                    }
                    .navigationDestination(for: String.self, destination: { country in
                        CountryView(country: country)
                    })
                    .navigationDestination(for: Int.self, destination: { i in
                        Text("View \(i)")
                    })
                    .navigationTitle("NavigationStack")
                }

    }
}

struct CountryView: View {
    var country: String
    
    init(country: String) {
        self.country = country
        print("【Log】: \(country)")
    }
    
    var body: some View {
        Text(country)
    }
}


struct HHNavigationStack_Previews: PreviewProvider {
    static var previews: some View {
        HHNavigationStack()
    }
}
